import java.util.concurrent.CopyOnWriteArrayList;


public class demo3 {
    public static void main(String[] args) {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();

        // 向列表中添加元素的线程
        Thread writer = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                list.add(i);
                System.out.println("Added: " + i);
            }
        });

        // 读取列表的线程
        Thread reader = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("Current List: " + list);
                try {
                    Thread.sleep(100); // 模拟一些延迟
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        writer.start();
        reader.start();
    }
}
